package com.ruoyi.mall.app.service.impl;


import com.ruoyi.mall.app.service.DtsArticleService;
import com.ruoyi.mall.common.domain.DtsArticle;
import com.ruoyi.mall.common.mapper.DtsArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class DtsArticleServiceImpl implements DtsArticleService {

    @Autowired
    private DtsArticleMapper dtsArticleMapper;

    @Autowired
    public RedisTemplate redisTemplate;

    @PostConstruct
    public void init()
    {
        List<DtsArticle>  list = dtsArticleMapper.selectDtsArticleList(null);
        redisTemplate.opsForValue().set("articleList", list);
    }

    @Override
    public List<DtsArticle> selectAll(DtsArticle dtsArticle) {
        if (dtsArticle.getId() == null) {
            List<DtsArticle> list = (List<DtsArticle>) redisTemplate.opsForValue().get("articleList");
            if (list != null) {
                return list;
            }
        }

        List<DtsArticle> tmp = dtsArticleMapper.selectDtsArticleList(dtsArticle);
        if (tmp != null) {
            redisTemplate.opsForValue().set("articleList", tmp);
        }
        return dtsArticleMapper.selectDtsArticleList(dtsArticle);
    }
}
